package br.com.sigad.servlets.procurar;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONValue;

import br.com.sigad.persistencia.dao.documento.DocumentoDAO;
import br.com.sigad.servlets.util.RecuperadorDeParametrosDaRequisicao;

/**
 * Servlet implementation class ProcurarDocumento
 */
@WebServlet("/ProcurarDocumento")
public class ProcurarDocumento extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Map<String, String> parametros = new RecuperadorDeParametrosDaRequisicao(request).getParametros();

		DocumentoDAO dao = new DocumentoDAO();
		List<Map<String, String>> resultado = dao.procurarDocumentos(parametros.get("texto"));
		String jsonString = JSONValue.toJSONString(resultado);

		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(String.valueOf(jsonString));
	}
}
